package org.xzx.cloud.ad.index;


import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import org.xzx.cloud.ad.dump.DataTableConstant;
import org.xzx.cloud.ad.dump.table.*;
import org.xzx.cloud.ad.handler.AdLevelDataHandler;
import org.xzx.cloud.ad.mysql.constant.OpType;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO://{@link DependsOn}表示一个 bean A的实例化依赖另一个 bean B 实例化，但是 A 并不需要持有一个 B 的对象
 *
 * 加载全量索引
 *
 * @author xzx
 */
@Component
@DependsOn("dataTable")
public class IndexFileLoader {

    private List<String> loadDumpData(String fileName) {
        try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) {
            return br.lines().collect(Collectors.toList());
        } catch (IOException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

    /**
     * Spring 容器启动时执行该方法，从所有文件[先从数据库中导出某个时间点前的广告数据，该时间点之后的广告数据采用 Binlog 进行增量索引]中加载全量广告数据构建索引
     */
    @PostConstruct
    public void init() {
        List<String> adPlanStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_PLAN)
        );
        adPlanStrings.forEach(p -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(p, AdPlanTable.class), OpType.ADD)
        );

        List<String> adCreativeStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_CREATIVE)
        );
        adCreativeStrings.forEach(c -> AdLevelDataHandler.handleLevel2(
                JSON.parseObject(c, AdCreativeTable.class), OpType.ADD)
        );

        List<String> adUnitStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_UNIT)
        );
        adUnitStrings.forEach(u -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(u, AdUnitTable.class), OpType.ADD
        ));

        List<String> adCreativeUnitStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_CREATIVE_UNIT)
        );
        adCreativeUnitStrings.forEach(cu -> AdLevelDataHandler.handleLevel3(
                JSON.parseObject(cu, AdCreativeUnitTable.class), OpType.ADD
        ));

        List<String> adUnitDistrictStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_UNIT_DISTRICT)
        );
        adUnitDistrictStrings.forEach(d -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(d, AdUnitDistrictTable.class), OpType.ADD
        ));

        List<String> adUnitItStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_UNIT_IT)
        );
        adUnitItStrings.forEach(i -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(i, AdUnitItTable.class), OpType.ADD
        ));

        List<String> adUnitKeywordStrings = loadDumpData(
                String.format("%s%s", DataTableConstant.DATA_ROOT_DIR, DataTableConstant.AD_UNIT_KEYWORD)
        );
        adUnitKeywordStrings.forEach(k -> AdLevelDataHandler.handleLevel4(
                JSON.parseObject(k, AdUnitKeywordTable.class), OpType.ADD
        ));
    }
}
